package VirtualMachines;

import java.util.Random;

public class ECU {
	static private boolean isTurnOn;
	static private int velocity;
	static private boolean signal;
	
	public ECU(){
		isTurnOn = true;
		velocity = 50;
		signal = false;
		
		TurnOn();
	}
	
	static public void TurnOn(){
		new Thread(new Runnable() {
			public void run() {
				try {
					while(isTurnOn){
						getCurrentVeloctiy();
//						System.out.println(showECUData());
						Thread.sleep(1000);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		}).start();
	}
	
	static public boolean isTurnOn(){
		return isTurnOn;
	}
	
	static public int getCurrentVeloctiy(){
		if(velocity < 1){
			velocity++;
		}
		else{
			Random random = new Random();
			
			if(random.nextBoolean())
				velocity++;
			else
				velocity--;
		}
		return velocity;
	}
	
	static public void signal(boolean signalFromControlPanel){
		signal = signalFromControlPanel;
	}
	
	static public String showECUData(){
		String result;
		String signalStr;
		if(signal)
			signalStr = "Breaking!";
		else
			signalStr = "No Signal";
		
		result = "Velocity : " + velocity + "\t" + "Signal :" + signalStr;
		return result;
	}
}
